import { getTokenByAuthCode } from '@/api/auth'
import { getToken, setToken } from '@/utils/auth'
import { defineStore } from 'pinia'


const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken(),
    id: '',
    name: '',
    avatar: '',
    organizationName: '',
  }),

  actions: {
    // 登录
    getTokenByAuthCodeAction(params: Object) {
      return new Promise((resolve, reject) => {
        getTokenByAuthCode(params).then((response: any) => {
          if (response.code === 1) {
            setToken(response.data.accessToken.tokenValue)
            this.token = response.data.accessToken.tokenValue
            resolve(response)
          } else {
            reject(response.message || '登录接口异常，请联系管理员')
          }
        }).catch(error => {
          // http请求异常
          reject(error.message || '登录接口异常，请联系管理员')
        })
      })
    }
  }
})

export default useUserStore